package com.eascs.admin.o2o.oaotodata.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.utils.StringUtils;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanUserEntity;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.admin.o2o.loan.trans.O2oLoanUserTransFormart;
import com.eascs.admin.o2o.loanAccount.service.Io2oLoanapplyAccountService;
import com.eascs.admin.o2o.oaotodata.entity.OaO2oLoanapply;
import com.eascs.admin.o2o.oaotodata.form.SearchBillForm;
import com.eascs.admin.o2o.oaotodata.service.OaO2oDataService;
import com.eascs.admin.o2o.oaotodata.trans.O2oOATransEntity;
import com.eascs.admin.recordlog.constant.O2oLogType;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StopWatch;
import com.eascs.crm.common.custnew.entity.CustomerEntity;
import com.eascs.crm.common.custnew.service.CustomerService;
import com.eascs.crm.common.service.ICustomerInfoService;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;

import net.sf.json.JSONObject;

/**
* @author bao.pan 2016年12月26日 上午11:10:41
*/
@RestController
@RequestMapping("/oaoro2odatasub")
public class OaOrO2oDataSubmitController extends BaseRestController {
	private static Logger logger = LoggerFactory.getLogger(OaOrO2oDataSubmitController.class);
	@Autowired
	OaO2oDataService oaO2oDataService;
	@Reference
	ICustomerInfoService iCustomerInfoService;
	@Reference
	CustomerService customerService;
	@Autowired
	private O2oLoanapplyService o2oLoanapplyService;
	private String billcode;
	 protected HttpServletResponse response;
	 
	 
	 @Autowired
	 Io2oLoanapplyAccountService o2oLoanapplyAccountService;
	 
	@RequestMapping("/startbuttonoa")
	public BaseRestResult<String> StartButton(String type) {
		/*List<OaO2oLoanapply> list=	oaO2oDataService.OaOrO2oloanapply();
//		for (OaO2oLoanapply oaO2oLoanapply : list) {
//			System.out.println("aa="+oaO2oLoanapply.getLoanapplyno());
//		}
		int i=1;
		for (OaO2oLoanapply oa : list) {
			// 获取用户的信息
			CustomerEntity customer = customerService.queryCustomerEntityByIdCard(oa.getIdcard());
			System.out.println(i++ +";"+oa.getLoanapplyno()+";"+oa.getBillcode());
			logger.info(i++ +";"+oa.getLoanapplyno()+";"+oa.getBillcode());
			if (customer!=null) {
				oa.setLoanId(customer.getCustomerId());
			}else{
				oa.setLoanId("0");
			}
			O2oLoanapplyEntity o2o=O2oOATransEntity.OAorO2oLoanapplyEntity(oa);
//			int num=oaO2oDataService.INSERTloanapply(o2o);
			o2o.setBcFlag("N");
			o2o.setIsCheckMqCl("Y");
			o2o.setContractFlag("Y");
			if (oa.getPayeaid()==null) {
				oa.setPayeaid(0);
			}
			if (oa.getPayeaid()>0 && O2oLogType.LOAN_STATUS_O.equals(oa.getWfType())) {
				String data=oaO2oDataService.findpaydate(oa.getPayeaid());
				 Date d=null;
				 try {
					d=DateFormatUtils.ISO_DATE_FORMAT.parse(data);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				o2o.setLoanFlg(StatusContexts.O2O_YES);
				o2o.setPaymentTime(d);
				Date dd=StringUtils.datebyMM(d, Integer.valueOf(oa.getLoanterm()));
				//System.out.println("1放款时间："+DateFormatUtils.ISO_DATE_FORMAT.format(d)+";月："+oa.getLoanterm()+";到期时间："+DateFormatUtils.ISO_DATE_FORMAT.format(dd));
				o2o.setEndBankDate(new java.sql.Date(dd.getTime()));
			}else if (O2oLogType.LOAN_STATUS_O.equals(oa.getWfType())) {
				o2o.setLoanFlg(StatusContexts.O2O_YES);
				Date dd=StringUtils.datebyMM(oa.getPaymentTime(), Integer.valueOf(oa.getLoanterm()));
				//System.out.println("2放款时间："+DateFormatUtils.ISO_DATE_FORMAT.format(oa.getPaymentTime())+";月："+oa.getLoanterm()+";到期时间："+DateFormatUtils.ISO_DATE_FORMAT.format(dd));
				o2o.setEndBankDate(new java.sql.Date(dd.getTime()));
			}else {
				o2o.setLoanFlg(StatusContexts.O2O_NO);
			}
			O2oLoanUserEntity o2oLoanUserEntity = O2oLoanUserTransFormart.customerToloanUser(new O2oLoanUserEntity(), customer, o2o.getLoanapplyNo());
			o2oLoanUserEntity.setBankApplyno(oa.getBankApplyno());
			o2oLoanUserEntity.setBankStatus(oa.getBankStatus());
			o2oLoanUserEntity.setNotation(oa.getNotation());
			o2oLoanUserEntity.setType(oa.getType());
			o2oLoanUserEntity.setBackTime(oa.getBackTime());
			if (customer != null && customer.getMateId() != null) {
				// 获取用户的信息
				CustomerEntity spouseEntity = customerService.queryCustomerEntityByCustomerId(customer.getMateId());
				if (spouseEntity==null) {
					o2oLoanapplyService.saveEntityById(o2o, o2oLoanUserEntity);
				}else{
					O2oLoanUserEntity o2oLoanUserSpouseEntity = O2oLoanUserTransFormart.customerToloanUser(new O2oLoanUserEntity(), spouseEntity, o2o.getLoanapplyNo());
					o2oLoanUserSpouseEntity.setSpouseId(o2oLoanUserEntity.getLoanId());
					o2oLoanapplyService.saveEntityById(o2o, o2oLoanUserEntity, o2oLoanUserSpouseEntity);
				}
			} else {
				o2oLoanapplyService.saveEntityById(o2o, o2oLoanUserEntity);
			}
		}
		*/
		 return BaseRestResult.info("成功");
	}
	@RequestMapping("/edit")
	public HtmlRestResult editInternal(){
			return HtmlRestResult.popHtmlFragment("oaoro2o/v");
	}
	@RequestMapping("/se")
	public HtmlRestResult se(){
			return HtmlRestResult.popHtmlFragment("oaoro2o/v1");
	}
	@RequestMapping("/billcode")
	public HtmlRestResult billcode(String billcode){
		billcode=this.billcode;
		System.out.println(billcode);
		ViewContext.put("billcode", billcode);
		//BaseRestResult.info("确定成功！").andCloseActionDialog().
		return HtmlRestResult.html("oaoro2o/billcode");
	}
	@RequestMapping("/add")
	public BaseRestResult<String> add(String billcode){
		System.out.println(billcode);
		this.billcode=billcode;
		//BaseRestResult.info("确定成功！").andCloseActionDialog().
		return BaseRestResult.info("确定成功！").andCloseActionDialog().andJquerySelectAjaxAction("#add_billcode");
	}

	@RequestMapping("/searchBillByWord")
	public BaseRestResult<List<Object[]>> searchBill(SearchBillForm searchBillForm) {
		List<Object[]> objsList = new ArrayList<>();
		if (!"abc".equals(searchBillForm.getWord())) {
			objsList.add(new Object[] { "id001","code001", "借款人：借款人1，身份证：身份证1，放款日期：2016-09-12", "借款人姓名1" });
			objsList.add(new Object[] { "id002", "code002", "借款人：借款人3，身份证：身份证3，放款日期：2015-09-12", "借款人姓名3" });
		}
		return BaseRestResult.buildRestResult(objsList);
	}
	/**
	 * 未放款有收费未退
	 */
	@RequestMapping("/acc1")
	public BaseRestResult<String> acc1(){
		StopWatch stopWatch = new StopWatch();
		logger.info("未放款有收费未退任务开始......");
		oaO2oDataService.findwfByPage1();
		logger.info("未放款有收费未退任务任务结束，共耗时：[{}]秒", stopWatch.elapsedTime() / 1000);
		return BaseRestResult.warn("确定成功！");
	}
	/**
	 * 已放款未结清有收费未退
	 */
	@RequestMapping("/acc2")
	public BaseRestResult<String> acc2(){
		StopWatch stopWatch = new StopWatch();
		logger.info("已放款未结清有收费未退任务开始......");
		oaO2oDataService.findwfByPage2();
		logger.info("已放款未结清有收费未退任务任务结束，共耗时：[{}]秒", stopWatch.elapsedTime() / 1000);
		return BaseRestResult.warn("确定成功！");
	}
	/**
	 * 已结清有收费未退
	 */
	@RequestMapping("/acc3")
	public BaseRestResult<String> acc3(){
		StopWatch stopWatch = new StopWatch();
		logger.info("已结清有收费未退任务开始......");
		oaO2oDataService.findwfByPage3();
		logger.info("已结清有收费未退任务任务结束，共耗时：[{}]秒", stopWatch.elapsedTime() / 1000);
		return BaseRestResult.warn("确定成功！");
	}
	
	@RequestMapping("/o2o/scheduler/OpenLoanAccount")
	public void OpenLoanAccount() {
		StopWatch stopWatch = new StopWatch();
		logger.info("创建单款单账户任务开始......");
		o2oLoanapplyAccountService.OpenLoanAccount();
		logger.info("创建贷款单账户任务任务结束，共耗时：[{}]秒", stopWatch.elapsedTime() / 1000);
	}
	
	
}
